package com.iemes.controller.qc;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CorrelationDetection;
import com.iemes.entity.MaterialQualityForMap;
import com.iemes.entity.ProjectJcForMmap;
import com.iemes.mapper.MaterialQualityMapper;
import com.iemes.mapper.MeasurementTestMapper;
import com.iemes.mapper.ProjuctJcMapper;
import com.iemes.util.Common;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

//来料检验单
@Controller
@RequestMapping("/mq/")
public class MaterialQualityController extends BaseController {
    @Inject
    private MaterialQualityMapper materialQualityMapper;

    @Inject
    private ProjuctJcMapper projuctJcMapper;

    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/qc/mq/mq_list";
    }

    //获取分页数据
    @ResponseBody
    @RequestMapping("getMqByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort) throws Exception {
        String myValue = getPara("myValue");
        if(myValue == null || myValue.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            PageInfo pageInfo = new PageInfo<>( materialQualityMapper.getAll());
            return pageInfo;

        }else {
            String[] split = myValue.split(",");
            if(split.length > 1){
                if(split[0].equals("name")){
                    //根据名称进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(materialQualityMapper.getData(split[1],"name"));
                    return pageInfo;
                }else {
                    //根据编号进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(materialQualityMapper.getData(split[1],"code"));
                    return pageInfo;
                }
            }else {
                PageInfo pageInfo = new PageInfo<>( materialQualityMapper.getAll());
                return pageInfo;
            }
        }
    }

    //打开添加页面
    @RequestMapping("openAdd")
    public String openProcessManagementUI(){
        return Common.BACKGROUND_PATH + "/qc/mq/mq_add";
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addMq")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addOrder(){
        MaterialQualityForMap materialQualityForMap = getFormMap(MaterialQualityForMap.class);
        try {
            materialQualityMapper.addEntity(materialQualityForMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("openEdit")
    public String openEditUi(Model model){
        String id = getPara("id");
        MaterialQualityForMap materialQualityForMap = materialQualityMapper.findbyFrist("id", id, MaterialQualityForMap.class);
        //根据编码获取内容
        List<CorrelationDetection> detection = materialQualityMapper.getCd(materialQualityForMap.get("detection_code").toString());

        List<CorrelationDetection> materialId = materialQualityMapper.getMaterialId(detection.get(0).get("detection_code").toString());

        model.addAttribute("mq",materialQualityForMap);

        model.addAttribute("cd",materialId);
        //获取
        return Common.BACKGROUND_PATH + "/qc/mq/mq_edit";
    }


    //保存修改数据
    @ResponseBody
    @RequestMapping("editMq")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editUn() throws Exception {
        MaterialQualityForMap materialQualityForMap = getFormMap(MaterialQualityForMap.class);
        materialQualityMapper.deleteByAttribute("id", materialQualityForMap.get("id")+"", MaterialQualityForMap.class);
        materialQualityMapper.addEntity(materialQualityForMap);
        return "success";
    }

    @ResponseBody
    @RequestMapping("delMq")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息删除")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delMq() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            baseMapper.deleteByAttribute("id", id, MaterialQualityForMap.class);
        }
        return "success";
    }

    @RequestMapping("mqProduct")
    public String product(Model model){
        //获取相关的项目信息然之后分割字符串之后根据检验内容进行查询
        String id = getPara("id");
        //根据id获取数据
        List<MaterialQualityForMap> ma = materialQualityMapper.getId(id);;
        //获取数据
        String name = ma.get(0).get("detection_project").toString();
        //分割字符串
        String[] split = name.split(",");
        List<ProjectJcForMmap>  pjList = new ArrayList<>();
        if(split.length > 0){
            for (String n : split) {
                List<ProjectJcForMmap> content = projuctJcMapper.getContent(n);
                pjList.add(content.get(0));
            }
        }
        model.addAttribute("list",pjList);
        return Common.BACKGROUND_PATH + "/qc/mq/mq_product";
    }
    //查询数据
    @ResponseBody
    @RequestMapping("getRelevanceData")
    public String getRelevanceData(){
        return JSON.toJSONString(projuctJcMapper.selRelevanceName(getPara("id")));
    }

    //添加关联数据
    @ResponseBody
    @RequestMapping("insertData")
    public String insetData(){
        String myid = getPara("myid");
        String detection_code = getPara("detection_code");
        String materialName = getPara("materialName");
        String record = getPara("record");
        String no_number = getPara("no_number");
        String qualified = getPara("qualified");
        String inlineCheckbox1 = getPara("inlineCheckbox1");
        //将数据存放
        materialQualityMapper.insertData(detection_code,materialName,record,no_number,qualified,inlineCheckbox1,myid);
        //放入数据
        return "success";
    }

    //添加关联数据
    @ResponseBody
    @RequestMapping("upDatafx")
    public String upDatafx(){
        String myid = getPara("myid");
        String detection_code = getPara("detection_code");
        String materialName = getPara("materialName");
        String record = getPara("record");
        String no_number = getPara("no_number");
        String qualified = getPara("qualified");
        String inlineCheckbox1 = getPara("inlineCheckbox1");
        //将数据存放
        //删除数据
        materialQualityMapper.delCd(detection_code);
        materialQualityMapper.insertData(detection_code,materialName,record,no_number,qualified,inlineCheckbox1,myid);
        //放入数据
        return "success";
    }
}
